package com.yupi.yupao.service.play;

import java.util.concurrent.CompletableFuture;
public class CompletableFutureExample {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Task 1 completed.";
        });

        CompletableFuture<String> future2 = future.thenApplyAsync(result -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return result + " Task 2 completed.";
        });

        // 链接第三个任务，在第二个任务完成后执行
        CompletableFuture<Void> future3 = future2.thenAcceptAsync(result -> {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(result + " Task 3 completed.");
        });
        System.out.println("Continuing with other work.");
        future3.join();
    }
}